home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / visulztn / lise / minilise.lha / minilise / auxcfg.h < prev    next >
C/C++ Source or Header  |  1993-03-30  |  4KB  |  134 lines

  1. /*
  2.   This include file should be used when accessing auxillary devices.
  3.   Routines defined:
  4.      int auxopen(name) char name[]
  5.          auxerr(string)
  6.   auxopen returns a file descriptor for use with read and write.
  7.   The global array "auxparams" is used to store device specific information.
  8.   auxerr should be called, whenever the device returns an error message.
  9. */
  10.  
  11. char *auxerrprg = NULL,
  12.      *auxerrmsg = NULL,
  13.      *auxdatafile = NULL,
  14.      *auxparams[20];
  15.  
  16. auxopen(name)
  17. char name[];
  18. {
  19. int i,n,m,fd;
  20. char c,*s, *z;
  21. char *tty;
  22. FILE *fp;
  23. char *home;
  24.  
  25.    s = (char *) malloc(80);
  26.    z = (char *) malloc(80);
  27.    tty = (char *) malloc(80);
  28.    auxerrprg = (char *) malloc(80);
  29.    auxerrmsg = (char *) malloc(80);
  30.    auxdatafile = (char *) malloc(80);
  31.    for(n=0;n<20;n++) {
  32.       auxparams[n] = (char *) malloc(80);
  33.       strcpy(auxparams[n],"");
  34.    }
  35. #ifdef UNIX
  36. /* get name of tty */
  37.    system("whoami >/usr/tmp/whoami");
  38.    fp = fopen("/usr/tmp/whoami","r");
  39.    fgets(tty,80,fp);
  40.    fclose(fp); unlink("/usr/tmp/whoami");
  41. #endif
  42.    fp=fopen("Aux_Config","r");
  43. #ifdef UNIX
  44.    if(fp==NULL) {
  45.       home = (char *) getenv("HOME");
  46.       s[0]=0;
  47.       if(home != NULL) {strcpy(s,home); strcat(s,"/"); }
  48.       strcat(s,"Aux_Config");
  49.       fp=fopen(s,"r");
  50.    }
  51.    if(fp==NULL) {
  52.       home = (char *) getenv("LISEPRG");
  53.       s[0]=0;
  54.       if(home != NULL) {strcpy(s,home); strcat(s,"/"); }
  55.       strcat(s,"Aux_Config");
  56.       fp=fopen(s,"r");
  57.    }
  58. #endif
  59. #ifdef AMIGA
  60.    if(fp==NULL) fp=fopen("LISE:Aux_Config","r");
  61. #endif
  62.    if(fp==NULL) {
  63.       fprintf(stderr,"auxopen: can not find Aux_Config file\n");
  64.       exit(-1);
  65.    }
  66.    while(!feof(fp)) {
  67.       c=fgetc(fp);
  68.       if(c==';') while(c!=10) c=fgetc(fp);            /* skip comment */
  69.       if(c==':') {                                    /* new entry */
  70.          fscanf(fp,"%s\n",s);
  71.          if(strcmp(s,name)==0) {                      /* entry found */
  72.             n=0; m=0;
  73.             while(c!='#') {
  74.                 c=fgetc(fp);
  75.                 if(c=='#') break;
  76.                 if(c==10) {
  77.                   auxparams[n++][m]=0;
  78.                   m=0;
  79.                   continue;
  80.                }
  81.                if(c==';') {                           /* skip comment */
  82.                   while(c!=10) c=fgetc(fp);
  83.                   auxparams[n++][m]=0;
  84.                   m=0;
  85.                   continue;
  86.                } else {                               /* store string */
  87.                   auxparams[n][m++]=c;
  88.                }
  89.             }
  90.          } else {                                     /* not the right entry */
  91.             while(c!='#') {
  92.                while(c!=10) c=fgetc(fp);
  93.                c=fgetc(fp);
  94.             }
  95.          }
  96.       }
  97.    }
  98.    fclose(fp);
  99.    if(auxparams[0][0]==0) {
  100.       fprintf(stderr,"sorry, device entry >%s< not found in Aux_Config\n",name);
  101.       exit(-1);
  102.    }
  103.    for(n=0;n<20;n++) {
  104.       for(m=strlen(auxparams[n])-1;(auxparams[n][m]==32);m--) auxparams[n][m]=0;
  105.       m = 0; while(auxparams[n][m]==32) m++;
  106.       i = 0; while(auxparams[n][m]!=0) auxparams[n][i++] = auxparams[n][m++];
  107.       auxparams[n][i++] = 0;
  108. #ifdef UNIX
  109.       i = instr("@",auxparams[n]);
  110.       if(i >= 0) {
  111.          s[0] = 0;
  112.          midstr(s,auxparams[n],0,i - 1);
  113.          strcat(s,tty);
  114.          midstr(z,auxparams[n],i + 1,strlen(auxparams[n]));
  115.          strcat(s,z); strcpy(auxparams[n],s);
  116.       }
  117. #endif      
  118.    }
  119.    fd=open(auxparams[0],2,0);
  120.    strcpy(auxerrprg,auxparams[1]);
  121.    strcpy(auxerrmsg,auxparams[2]);
  122.    strcpy(auxdatafile,auxparams[3]);
  123.    free(s); free(z); free(tty);
  124.    return(fd);
  125. }
  126.  
  127. void auxerr(string)
  128. char string[];
  129. {
  130.    system(auxerrprg);
  131.    fprintf(stderr,"%s  :  %s\n",auxerrmsg,string);
  132. }
  133.  
  134.